package com.gaoaoy.finance.biz.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.UUID;
import com.gaoaoy.finance.biz.constant.RedisKeyConstant;
import com.gaoaoy.finance.biz.service.MemberLoginService;
import com.gaoaoy.finance.biz.service.dto.form.GetBase64CodeForm;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author gaoaoy
 * @version 1.0.0
 * @create 2024/3/24 16:56
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class MemberLoginServiceImpl implements MemberLoginService {

    final RedisTemplate<String, String> redisTemplate;

    /**
     * 获取客户端ID
     *
     * @return 客户端ID
     */
    @Override
    public String getClientId() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 获取图形验证码
     *
     * @return 验证码
     */
    @Override
    public String getBase64Code(GetBase64CodeForm form) {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(300, 192, 5, 1000);
        String code = lineCaptcha.getCode();
        // 将图形验证码写到redis缓存中
        redisTemplate.opsForValue().set(RedisKeyConstant.GRAPHIC_VERIFICATION_CODE + form.getClientId(),
                code, 15, TimeUnit.MINUTES);
        log.info("图形验证码{}", code);
        return lineCaptcha.getImageBase64();
    }

}
